/*
 * 对于动画效果可以进行控制，
 * 参数是anim_one_frame(渐变的间隙时间)
 * anim_total(一次淡入淡出的总的渐变数)
 */
package com.android.sq.zouqi.login;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.android.sq.zouqi.Constants;


public class HandlerThread_ImageView extends Thread {
	public int anim_time = 2; 	// 2秒
	public boolean is_anim = true;
    public Handler handler;
    public Message msg;
    private boolean is_run = true;
    private long start_time;
    private long current_time;
    private int anim_one_frame = 20;	// 30ms变一次	
    private int anim_count = 0;
    private int anim_total = 2000 / anim_one_frame;	// 淡入淡出的一共次数 2秒/20,次数为100

    public HandlerThread_ImageView(Handler handler) {
        super();
        this.handler = handler;
        start_time = System.currentTimeMillis();
    }
    @Override
    public void run() {
        while(is_run){
        	//Log.e("BA", "HandlerThread IS RUNNING");
            current_time = System.currentTimeMillis();
            // 显示淡入淡出效果
            animation(current_time);
            animationFinish();
        }
    }
    
    // 发送消息给handler，让handler更新ImageView的显示
    public void animation(long current_time) {
    	if(current_time - start_time >= anim_one_frame && is_anim) {
    		anim_count++;
    		Message msg = new Message();
    		if (anim_count <= (anim_total / 2)) {
    			msg.what = Constants.ANIMATION_FADE_IN;
    		} else {
    			msg.what = Constants.ANIMATION_FADE_OUT;
    		}
    		handler.sendMessage(msg);
    		
    		start_time = current_time;
    	}
    }
    
    // 当一个动画周期已到，发消息给handler，让handler进行更新
    public void animationFinish() {
    	if (anim_count == anim_total && is_anim) {
            Message msg=new Message();
            msg.what = Constants.ANIMATION_TIME_UP;
            // 通知handler 此次动画的周期已到
            handler.sendMessage(msg);
            // 设置不能进行下一次动画周期
            is_anim = false;
        }
    }
    
    public void setTotal(int animation_time) {
    	this.anim_time = animation_time;
    }
    
    public void setIsRun(boolean is_run) {
    	this.is_run = is_run;
    }
    
    public boolean getIsRun() {
    	return is_run;
    }
    
    public void setStartTime(long start_time) {
    	this.start_time = start_time;
    }
    
    public void setIsAnim(boolean is_anim) {
    	this.is_anim = is_anim;
    }
    
    public void setAnimCount(int anim_count) {
    	this.anim_count = 0;
    }
}
